package com.management.vtribe.service;

import com.management.vtribe.Bean.Category;
import com.management.vtribe.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    public List<Category> getAllCategory(){
        return categoryMapper.getAllCategory();
    }

    public  List<Category> getSomeCategory(int page,int num){
        return categoryMapper.getSomeCategory(page*num, num);
    }

    public int getCountOfCategory(){
        return categoryMapper.getCountOfCategory();
    }

    public  int insertCategory(String name){
        int res = categoryMapper.updateCategoryByName(name);
        if(res==1){
            return res;
        }else{
            return categoryMapper.insertCategory(name);
        }
    }

    public int updateCategoryById(int id,String name){
        List<Category> categories = categoryMapper.getAllCategory();
        for (Category category:categories) {
            if(category.getName().equals(name)){
                return 0;
            }
        }
        return categoryMapper.updateCategoryById(id, name);
    }

    public int deleteCategoryById(int[] ids){
        return categoryMapper.deleteCategoryById(ids);
    }
}
